const Router = require("koa-router");
const router = new Router();
const md5 = require("md5-node");

router.get("/login", async ctx => {
  // 渲染页面
  await ctx.render("admin/login", { URL_PATH: ctx.config.URL_PATH });
});

router.post("/login", async ctx => {
  // 处理请求
  let { username, password } = ctx.request.body;
  password = md5(password);
  console.log(password);
  try {
    const data = await ctx.db.query(
      `SELECT user FROM admin WHERE user = '${username}' AND password = '${password}' LIMIT 1`
    ); // limit 1 防止注入
    if (data.length > 0) {
      ctx.body = "登录成功";
      // 可在这做成接口返回
      // {
      //   code: 200,
      //   msg: "登录成功"
      // }
    } else {
      ctx.body = "登录失败";
    }
  } catch (err) {
    console.log(err);
    ctx.body = "查询失败";
  }
});

module.exports = router.routes();
